package top.continew.admin.open.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.continew.admin.open.model.req.SmsMessageReq;
import top.continew.admin.open.model.req.WxSubscribeMessageReq;
import top.continew.admin.open.service.NotifyService;

@Slf4j
@Service
public class NotifyServiceImpl implements NotifyService {

    @Resource
    private WxMaService wxMaService;

    @Override
    public void smsMessage(SmsMessageReq params) {

    }

    @Override
    public boolean wxSubscribeMessage(WxSubscribeMessageReq params) {
        WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
                .toUser(params.getOpenId())
                .templateId(params.getTemplateId())
                .page(params.getPageUrl())
                .build();
        // 设置将推送的消息
        params.getMsgContent().forEach((k, v) -> {
            wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.MsgData(k, v));
        });
        try {
            log.info("开始发送消息！！！！");
            wxMaService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage);
            log.info("消息发送成功！！！！");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
